﻿Public Class Calendar : Inherits PictureBox

	Friend SpecialDays As New SortedList(Of Date, String)

	Sub New()
		ResizeRedraw = True
		DoubleBuffered = True
	End Sub

	Dim _Year As Integer
	Property Year As Integer
		Get
			Return _Year
		End Get
		Set(ByVal Value As Integer)
			_Year = Value
			Bitmap = Nothing
		End Set
	End Property

	Dim Bitmap As Bitmap

	Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
		If Bitmap Is Nothing Then CreateBitmap()
		Dim R As Rectangle = ClientRectangle
		R.Inflate(-8, -8)
		e.Graphics.Clear(SystemColors.ButtonFace)
		e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
		e.Graphics.DrawImage(Bitmap, R)
	End Sub

	Private Sub CreateBitmap()
		Bitmap = New Bitmap(3200, 1800)
		Dim G As Graphics = Graphics.FromImage(Bitmap)
		G.Clear(SystemColors.ButtonFace)
		G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
		If Year < 1980 Then Exit Sub
		Dim B = New Rectangle(0, 0, Bitmap.Width, Bitmap.Height)
		For y = 0 To 2
			For x = 0 To 3
				Dim R As New Rectangle(B.Left + x * B.Width \ 4, B.Top + y * B.Height \ 3, B.Width \ 4, B.Height \ 3)
				R.Inflate(-12, -12)
				DrawMonth(G, R, y * 4 + x + 1)
			Next
		Next
		Invalidate()
	End Sub

	Private Function DrawMonth(ByVal g As Graphics, ByVal Rect As Rectangle, ByVal Month As Integer) As Integer
		Dim d As New Date(Year, Month, 1)
		g.FillRectangle(Brushes.White, Rect)
		g.DrawRectangle(New Pen(SystemColors.ControlDarkDark, 5), Rect)
		Dim TR = Rect
		TR.Height \= 7
		Dim Font As New Font(Parent.Font.Name, CSng(Rect.Height / 16), FontStyle.Bold)
		DrawText(g, Choose(Month, "OCAK", "ŞUBAT", "MART", "NİSAN", "MAYIS", "HAZİRAN", "TEMMUZ", "AĞUSTOS", "EYLÜL", "EKİM", "KASIM", "ARALIK") & " " & Year, Font, TR, Color.SteelBlue, "")
		Rect.Offset(0, TR.Height)
		Rect.Height -= TR.Height
		Rect.Inflate(-Rect.Width \ 16, -Rect.Height \ 16)
		Rect.Width = Int(Rect.Width / 7) * 7
		Rect.Height = Int(Rect.Height / 7) * 7
		'Rect.Inflate(-(Rect.Width - Int(Rect.Width / 7) * 7) / 2, -(Rect.Height - Int(Rect.Height / 7) * 7) / 2)
		Rect.Offset(1, 0)
		For y = 0 To 6
			For x = 0 To 6
				Dim R As New Rectangle(Rect.Left + x * Rect.Width \ 7, Rect.Top + y * Rect.Height \ 7, Rect.Width \ 7, Rect.Height \ 7)
				R.Width -= 1 : R.Height -= 1
				R.Inflate(-2, -2)
				Dim C = Color.Black
				If x > 4 Then C = Color.Red
				If y = 0 Then
					Font = New Font(Parent.Font.Name, CSng(Rect.Height / 15), FontStyle.Bold)
					g.FillRectangle(New SolidBrush(Color.FromArgb(64, Color.Black)), R)
					DrawText(g, "Pt.S.Ç.Pe.C.Ct.P".Split(".")(x), Font, R, C, "")
				Else
					Font = New Font(Parent.Font.Name, Rect.Height \ 18, FontStyle.Regular)
					Dim gun = (y - 1) * 7 + x + 1 - d.FirstDayOfMonth.DayOfWeek + 1
					If gun > 0 And gun <= d.LastDayOfMonth.Day Then
						Dim dd = d.AddDays(gun - 1)
						If Tatil(dd) Then C = Color.Red
						If SpecialDays.ContainsKey(dd) Then
							Dim a = SpecialDays(dd)
							Select Case a
								Case "i" : g.FillRectangle(New SolidBrush(Color.FromArgb(128, Color.Blue)), R)
								Case "h" : g.FillRectangle(New SolidBrush(Color.FromArgb(128, Color.Red)), R)
								Case "m" : g.FillRectangle(New SolidBrush(Color.FromArgb(128, Color.Orange)), R)
							End Select
						Else
							g.FillRectangle(New SolidBrush(Color.FromArgb(16, Color.Black)), R)
						End If
						DrawText(g, gun.ToString, Font, R, C, "")
					End If
				End If
				R.Inflate(-1, -1)
				'g.DrawRectangle(Pens.Blue, R)
			Next
		Next
	End Function

End Class
